home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / test / int.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  5.2 KB  |  197 lines  |  [TEXT/R*ch]

  1. (* test/int.sml -- here we test only the `exotic' operations
  2.    PS 1995-02-25, 1996-07-02 *)
  3.  
  4. use "auxil.sml";
  5.  
  6. local 
  7.     open Int
  8.     infix 7 quot rem
  9.     fun divmod (i, d, q, r)  = check(i div d = q andalso i mod d = r);
  10.     fun quotrem (i, d, q, r) = check(i quot d = q andalso i rem d = r);
  11. in    
  12.  
  13. val test1a = divmod(10, 3, 3, 1);
  14. val test1b = divmod(~10, 3, ~4, 2);
  15. val test1c = divmod(~10, ~3, 3, ~1);
  16. val test1d = divmod(10, ~3, ~4, ~2);
  17.  
  18. val test2a = quotrem(10, 3, 3, 1);
  19. val test2b = quotrem(~10, 3, ~3, ~1);
  20. val test2c = quotrem(~10, ~3, 3, ~1);
  21. val test2d = quotrem(10, ~3, ~3, 1);
  22.  
  23. val test3 = check(max(~5, 2) =  2 andalso max(5, 2) = 5);
  24. val test4 = check(min(~5, 3) = ~5 andalso min(5, 2) = 2);
  25.  
  26. val test5 = check(sign ~57 = ~1 andalso sign 99 = 1 andalso sign 0 = 0);
  27. val test6 = check(sameSign(~255, ~256) andalso sameSign(255, 256) 
  28.           andalso sameSign(0, 0));
  29.  
  30. val test12 = 
  31.     case (minInt, maxInt) of
  32.     (SOME mi, SOME ma) =>
  33.         check(sign mi = ~1 andalso sign ma = 1 
  34.           andalso sameSign(mi, ~1) andalso sameSign(ma, 1))
  35.       | (NONE, NONE)       => "OK"
  36.       | _                  => "WRONG";
  37.  
  38. fun chk f (s, r) = 
  39.     check'(fn _ => 
  40.        case f s of
  41.            SOME res => res = r
  42.          | NONE     => false)
  43.  
  44. fun chkScan fmt = chk (StringCvt.scanString (scan fmt))
  45.  
  46. val test13a = 
  47.     List.map (chk fromString)
  48.              [("10789", 10789),
  49.           ("+10789", 10789),
  50.           ("~10789", ~10789),
  51.           ("-10789", ~10789),
  52.           (" \n\t10789crap", 10789),
  53.           (" \n\t+10789crap", 10789),
  54.           (" \n\t~10789crap", ~10789),
  55.           (" \n\t-10789crap", ~10789),
  56.           ("0w123", 0),
  57.           ("0W123", 0),
  58.           ("0x123", 0),
  59.           ("0X123", 0),
  60.           ("0wx123", 0),
  61.           ("0wX123", 0)];
  62.  
  63. val test13b = 
  64.     List.map (fn s => case fromString s of NONE => "OK" | _ => "WRONG")
  65.        ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+", 
  66.         "+ 1", "~ 1", "- 1", "ff"];        
  67.  
  68. val test14a = 
  69.     List.map (chkScan StringCvt.DEC)
  70.              [("10789", 10789),
  71.           ("+10789", 10789),
  72.           ("~10789", ~10789),
  73.           ("-10789", ~10789),
  74.           (" \n\t10789crap", 10789),
  75.           (" \n\t+10789crap", 10789),
  76.           (" \n\t~10789crap", ~10789),
  77.           (" \n\t-10789crap", ~10789),
  78.           ("0w123", 0),
  79.           ("0W123", 0),
  80.           ("0x123", 0),
  81.           ("0X123", 0),
  82.           ("0wx123", 0),
  83.           ("0wX123", 0)];
  84.  
  85. val test14b = 
  86.     List.map (fn s => case StringCvt.scanString (scan StringCvt.DEC) s 
  87.                   of NONE => "OK" | _ => "WRONG")
  88.        ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+", 
  89.         "+ 1", "~ 1", "- 1", "ff"];        
  90.  
  91. val test15a = 
  92.     List.map (chkScan StringCvt.BIN)
  93.              [("10010", 18),
  94.           ("+10010", 18),
  95.           ("~10010", ~18),
  96.           ("-10010", ~18),
  97.           (" \n\t10010crap", 18),
  98.           (" \n\t+10010crap", 18),
  99.           (" \n\t~10010crap", ~18),
  100.           (" \n\t-10010crap", ~18),
  101.           ("0w101", 0),
  102.           ("0W101", 0),
  103.           ("0x101", 0),
  104.           ("0X101", 0),
  105.           ("0wx101", 0),
  106.           ("0wX101", 0)];
  107.  
  108. val test15b = 
  109.     List.map (fn s => case StringCvt.scanString (scan StringCvt.BIN) s 
  110.                   of NONE => "OK" | _ => "WRONG")
  111.        ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+", 
  112.         "+ 1", "~ 1", "- 1", "2", "8", "ff"];
  113.  
  114. val test16a = 
  115.     List.map (chkScan StringCvt.OCT)
  116.              [("2071", 1081),
  117.           ("+2071", 1081),
  118.           ("~2071", ~1081),
  119.           ("-2071", ~1081),
  120.           (" \n\t2071crap", 1081),
  121.           (" \n\t+2071crap", 1081),
  122.           (" \n\t~2071crap", ~1081),
  123.           (" \n\t-2071crap", ~1081),
  124.           ("0w123", 0),
  125.           ("0W123", 0),
  126.           ("0x123", 0),
  127.           ("0X123", 0),
  128.           ("0wx123", 0),
  129.           ("0wX123", 0)];
  130.  
  131. val test16b = 
  132.     List.map (fn s => case StringCvt.scanString (scan StringCvt.OCT) s 
  133.                   of NONE => "OK" | _ => "WRONG")
  134.        ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+", 
  135.         "+ 1", "~ 1", "- 1", "8", "ff"];
  136.  
  137. val test17a = 
  138.     List.map (chkScan StringCvt.HEX)
  139.              [("20Af", 8367),
  140.           ("+20Af", 8367),
  141.           ("~20Af", ~8367),
  142.           ("-20Af", ~8367),
  143.           (" \n\t20AfGrap", 8367),
  144.           (" \n\t+20AfGrap", 8367),
  145.           (" \n\t~20AfGrap", ~8367),
  146.           (" \n\t-20AfGrap", ~8367),
  147.           ("0w123", 0),
  148.           ("0W123", 0),
  149.           ("0x", 0),
  150.           ("0x ", 0),
  151.           ("0xG", 0),
  152.           ("0X", 0),
  153.           ("0XG", 0),
  154.           ("0x123", 291),
  155.           ("0X123", 291),
  156.           ("-0x123", ~291),
  157.           ("-0X123", ~291),
  158.           ("~0x123", ~291),
  159.           ("~0X123", ~291),
  160.           ("+0x123", 291),
  161.           ("+0X123", 291),
  162.           ("0wx123", 0),
  163.           ("0wX123", 0)];
  164.  
  165. val test17b = 
  166.     List.map (fn s => case StringCvt.scanString (scan StringCvt.HEX) s 
  167.                   of NONE => "OK" | _ => "WRONG")
  168.        ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+", 
  169.         "+ 1", "~ 1", "- 1"];
  170.  
  171.  
  172. local 
  173.     fun fromToString i = 
  174.     fromString (toString i) = SOME i;
  175.  
  176.     fun scanFmt radix i = 
  177.     StringCvt.scanString (scan radix) (fmt radix i) = SOME i;
  178.  
  179. in
  180. val test18 = 
  181.     check'(fn _ => range (~1200, 1200) fromToString);
  182.  
  183. val test19 = 
  184.     check'(fn _ => range (~1200, 1200) (scanFmt StringCvt.BIN));
  185.  
  186. val test20 = 
  187.     check'(fn _ => range (~1200, 1200) (scanFmt StringCvt.OCT));
  188.  
  189. val test21 = 
  190.     check'(fn _ => range (~1200, 1200) (scanFmt StringCvt.DEC));
  191.  
  192. val test22 = 
  193.     check'(fn _ => range (~1200, 1200) (scanFmt StringCvt.HEX));
  194. end
  195.  
  196. end
  197.